<!-- 表行 》 状态 -->

<template>
  <el-table-column v-bind="tableColumn">
    <template #header>
      <slot name="label">{{ label }}</slot>
    </template>

    <template #default="{ row }">
      <slot v-if="hasPrefix" :row="row" name="prefix">{{ prefix }}</slot>

      <slot :row="row" :label="label" :bind="bind" :text="getText(row)" :props="getProps(row)">
        <span v-bind="getProps(row)">{{ getText(row) }}</span>
      </slot>

      <slot v-if="hasSuffix" :row="row" name="suffix">{{ suffix }}</slot>
    </template>
  </el-table-column>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import mixRow from '../mixRow'
import ShowStatus from '../../show/ShowStatus'

const name = 'RowStatus'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name,

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [mixRow],

  /**
   * 组件导入 (Abc.vue + Abc/index.vue)
   */
  ...CORE.components(require.context('./', true, /vue$/)),

  /**
   * 属性注册 (抽取以便查阅)
   */
  props: CORE.WEB[name],

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 状态字典
     */
    STATUS: ShowStatus.computed.STATUS,
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 转换方法
     */
    trans: ShowStatus.methods.trans,

    /**
     * 获取属性对象
     */
    getProps(row) {
      const { className, diyClass, STATUS, bind, colorIndex, hasColor, hasBorder, hasBackground } = this
      const diyStr = diyClass ? ` ${diyClass}` : ''
      const keys = bind.replace(/\[(\d+)\]/g, '.$1').split('.')
      const value = keys.reduce((data, key) => CORE.isJSON(data) || CORE.isArray(data) ? data[key] : null, row)

      // 是否有颜色
      const color = (STATUS.find((item) => item[0] === value) || [])[colorIndex]
      if (!color) return { class: `${className}${diyStr}` }

      // 颜色是否有效
      const matched = CORE.DICT.COLOR.find(([cn, en]) => color.includes(cn) || color.includes(en))
      if (!matched) return { class: `${className}${diyStr}` }

      const props = { class: `${className}${diyStr} ${className}-${matched[1]}`, style: {} }
      if (hasColor) props.style.color = matched[2]
      if (hasBorder) props.style.borderColor = matched[3]
      if (hasBackground) props.style.backgroundColor = matched[4]
      return props
    },
  },
}
</script>
